<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入退货订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员用户名" prop="memberUsername">
        <el-input
          v-model="queryParams.memberUsername"
          placeholder="请输入会员用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货人姓名" prop="returnName">
        <el-input
          v-model="queryParams.returnName"
          placeholder="请输入退货人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货人电话" prop="returnPhone">
        <el-input
          v-model="queryParams.returnPhone"
          placeholder="请输入退货人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.return_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理时间" prop="handleTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.handleTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择处理时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理人" prop="handleMan">
        <el-input
          v-model="queryParams.handleMan"
          placeholder="请输入处理人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:apply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:apply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:apply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:apply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="退货订单id" align="center" prop="orderId" />
      <el-table-column label="退货书籍id" align="center" prop="bookId" />
      <el-table-column label="退货订单号" align="center" prop="orderSn" />
      <el-table-column label="会员用户名" align="center" prop="memberUsername" />
      <el-table-column label="退款金额" align="center" prop="returnMoney" />
      <el-table-column label="退货人姓名" align="center" prop="returnName" />
      <el-table-column label="退货人电话" align="center" prop="returnPhone" />
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.return_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="handleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="书籍图片" align="center" prop="bookImg" />
      <el-table-column label="书籍名称" align="center" prop="bookName" />
      <el-table-column label="书籍数量" align="center" prop="bookCount" />
      <el-table-column label="书籍价格" align="center" prop="bookPrice" />
      <el-table-column label="书籍实际支付价格" align="center" prop="bookRealPrice" />
      <el-table-column label="退货原因" align="center" prop="reason" />
      <el-table-column label="退货描述" align="center" prop="description" />
      <el-table-column label="描述图片集，以逗号隔开" align="center" prop="descPics" />
      <el-table-column label="处理备注" align="center" prop="handleNote" />
      <el-table-column label="处理人" align="center" prop="handleMan" />
      <el-table-column label="退货的收货人" align="center" prop="receiveMan" />
      <el-table-column label="收货时间" align="center" prop="receiveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.receiveTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货备注" align="center" prop="receiveNote" />
      <el-table-column label="收货电话" align="center" prop="receivePhone" />
      <el-table-column label="收货大学" align="center" prop="receiveUniversity" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:apply:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:apply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退货申请信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退货订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入退货订单id" />
        </el-form-item>
        <el-form-item label="退货书籍id" prop="bookId">
          <el-input v-model="form.bookId" placeholder="请输入退货书籍id" />
        </el-form-item>
        <el-form-item label="退货订单号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入退货订单号" />
        </el-form-item>
        <el-form-item label="会员用户名" prop="memberUsername">
          <el-input v-model="form.memberUsername" placeholder="请输入会员用户名" />
        </el-form-item>
        <el-form-item label="退款金额" prop="returnMoney">
          <el-input v-model="form.returnMoney" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退货人姓名" prop="returnName">
          <el-input v-model="form.returnName" placeholder="请输入退货人姓名" />
        </el-form-item>
        <el-form-item label="退货人电话" prop="returnPhone">
          <el-input v-model="form.returnPhone" placeholder="请输入退货人电话" />
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择处理状态">
            <el-option
              v-for="dict in dict.type.return_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理时间" prop="handleTime">
          <el-date-picker clearable size="small"
                          v-model="form.handleTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="书籍图片" prop="bookImg">
          <el-input v-model="form.bookImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="书籍名称" prop="bookName">
          <el-input v-model="form.bookName" placeholder="请输入书籍名称" />
        </el-form-item>
        <el-form-item label="书籍数量" prop="bookCount">
          <el-input v-model="form.bookCount" placeholder="请输入书籍数量" />
        </el-form-item>
        <el-form-item label="书籍价格" prop="bookPrice">
          <el-input v-model="form.bookPrice" placeholder="请输入书籍价格" />
        </el-form-item>
        <el-form-item label="书籍实际支付价格" prop="bookRealPrice">
          <el-input v-model="form.bookRealPrice" placeholder="请输入书籍实际支付价格" />
        </el-form-item>
        <el-form-item label="退货原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入退货原因" />
        </el-form-item>
        <el-form-item label="退货描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="描述图片集，以逗号隔开" prop="descPics">
          <el-input v-model="form.descPics" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理备注" prop="handleNote">
          <el-input v-model="form.handleNote" placeholder="请输入处理备注" />
        </el-form-item>
        <el-form-item label="处理人" prop="handleMan">
          <el-input v-model="form.handleMan" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="退货的收货人" prop="receiveMan">
          <el-input v-model="form.receiveMan" placeholder="请输入退货的收货人" />
        </el-form-item>
        <el-form-item label="收货时间" prop="receiveTime">
          <el-date-picker clearable size="small"
                          v-model="form.receiveTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货备注" prop="receiveNote">
          <el-input v-model="form.receiveNote" placeholder="请输入收货备注" />
        </el-form-item>
        <el-form-item label="收货电话" prop="receivePhone">
          <el-input v-model="form.receivePhone" placeholder="请输入收货电话" />
        </el-form-item>
        <el-form-item label="收货大学" prop="receiveUniversity">
          <el-input v-model="form.receiveUniversity" placeholder="请输入收货大学" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listApply, getApply, delApply, addApply, updateApply } from "@/api/order/apply";

  export default {
    name: "Apply",
    dicts: ['return_order_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 退货申请信息表格数据
        applyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderId: null,
          bookId: null,
          orderSn: null,
          memberUsername: null,
          returnMoney: null,
          returnName: null,
          returnPhone: null,
          status: null,
          handleTime: null,
          bookImg: null,
          bookName: null,
          bookCount: null,
          bookPrice: null,
          bookRealPrice: null,
          reason: null,
          description: null,
          descPics: null,
          handleNote: null,
          handleMan: null,
          receiveMan: null,
          receiveTime: null,
          receiveNote: null,
          receivePhone: null,
          receiveUniversity: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询退货申请信息列表 */
      getList() {
        this.loading = true;
        listApply(this.queryParams).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          orderId: null,
          bookId: null,
          orderSn: null,
          createTime: null,
          memberUsername: null,
          returnMoney: null,
          returnName: null,
          returnPhone: null,
          status: null,
          handleTime: null,
          bookImg: null,
          bookName: null,
          bookCount: null,
          bookPrice: null,
          bookRealPrice: null,
          reason: null,
          description: null,
          descPics: null,
          handleNote: null,
          handleMan: null,
          receiveMan: null,
          receiveTime: null,
          receiveNote: null,
          receivePhone: null,
          receiveUniversity: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加退货申请信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getApply(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改退货申请信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateApply(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addApply(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除退货申请信息编号为"' + ids + '"的数据项？').then(function() {
          return delApply(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('order/apply/export', {
          ...this.queryParams
        }, `apply_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
